import { constantRoutes, asyncRoutes } from "@/router"
const state = {
  // 存放筛选后的路由规则数组，目的是给左侧菜单栏使用
  routes: constantRoutes
}

const mutations = {
  setRoutes(state, routes) {
    state.routes = [...constantRoutes, ...routes]
  }
}

const actions = {
  // 根据用户菜单权限数据过滤动态路由数据，
  // 并将过滤结果追加到state的routes中
  filterRoutes({ commit }, menus) {
    const routes = asyncRoutes.filter(item => 
      menus.some(el => el === item.name))
    commit('setRoutes', routes)
    // 注意，将过滤结果返回出去，目的是给路由导航守卫处使用
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
